class Animation {
  constructor() {
    this.animationName = 'stand'
    this.animations = {
      stand: [],
      move: []
    }
    this.setup()
    this.texture = null
    this.framesIndex = 0
    this.frameCount = 5

    this.x = 100
    this.y = 200

  }

  setup() {
    for (let i = 1; i <= 10; i++) {
      let m = new Spirit(`img1/move/move${i}.png`)
      this.animations.move.push(m)
    }
    for (let i = 1; i <= 9; i++) {
      let s = new Spirit(`img1/stand/stand${i}.png`)
      this.animations.stand.push(s)
    }
  }

  setState(state) {
    this.animationName = state
  }

  move(x) {
    this.setState('move')
    this.x += x
  }

  get frames() {
    return this.animations[this.animationName]
  }

  update() {
    if (this.frameCount == 0) {
      this.frameCount = 5

      this.framesIndex = (this.framesIndex + 1) % this.frames.length
      this.texture = this.frames[this.framesIndex]
    }

    this.frameCount--
  }

  draw(ctx) {
    if (this.texture) {
      this.texture.x = this.x
      this.texture.y = this.y
      this.texture.draw(ctx)
    }
  }
}